package view;

import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JEditorPane;

import model.commands.flow.Script;

import org.jdesktop.swingx.JXTaskPane;

import resources.ResourcesFactory;

public class IndividualScriptPanel extends JXTaskPane {

	/**
	 * 
	 */
	private static final long serialVersionUID = -3834915772326518366L;
	protected static final String RUN_INDIVIDUAL_SCRIPT = "Run Individual Script";
	protected static final String REMOVE_INDIVIDUAL_SCRIPT = "Remove Individual Script";
	protected static final String MOVE_UP_INDIVIDUAL_SCRIPT = "Move Up Individual Script";
	protected static final String MOVE_DOWN_INDIVIDUAL_SCRIPT = "Move Down Individual Script";
	private Script script;
	private int indexOrder;
	
	public IndividualScriptPanel(Script script, int indexOrder){
		super();
		this.script = script;
		this.indexOrder = indexOrder;
		createUI();
	}

	private void createUI() {
		setTitle(script.getName());
		setIcon(ResourcesFactory.getScriptIcon());
		setSpecial(true);
		setScrollOnExpand(true);
		
        
		//Description
        JEditorPane descriptionArea = new JEditorPane("text/html", "<html>"+script.getDescription()+"</html>");
        descriptionArea.setOpaque(false);
        descriptionArea.setName("detailsArea");
        add(descriptionArea); 
       
        //Actions
        addCommonActions();
	}

	private void addCommonActions() {
		//Play
		Action playAction = new AbstractAction("Play",ResourcesFactory.getPlayIcon()) {
			
			private static final long serialVersionUID = 7430912940318934757L;

			@Override
			public void actionPerformed(ActionEvent e) {
				IndividualScriptPanel.this.firePropertyChange(RUN_INDIVIDUAL_SCRIPT, null, indexOrder);
			}
		};
		playAction.putValue(Action.SHORT_DESCRIPTION, "Runs this script individually.");
        add(playAction);
        
        //Remove
        Action removeAction = new AbstractAction("Remove",ResourcesFactory.getClearIcon()) {
			
			private static final long serialVersionUID = 7430912940318934757L;

			@Override
			public void actionPerformed(ActionEvent e) {
				IndividualScriptPanel.this.firePropertyChange(REMOVE_INDIVIDUAL_SCRIPT, null, indexOrder);
				
			}
		};
		removeAction.putValue(Action.SHORT_DESCRIPTION, "Removes the script from the list.");
        add(removeAction);
        
        //Move Up
        Action moveUpAction = new AbstractAction("Move Up",ResourcesFactory.getMoveUpIcon()) {
			
			private static final long serialVersionUID = 7430912940318934757L;

			@Override
			public void actionPerformed(ActionEvent e) {
				IndividualScriptPanel.this.firePropertyChange(MOVE_UP_INDIVIDUAL_SCRIPT, null, indexOrder);
				
			}
			
			@Override
			public boolean isEnabled() {
				return indexOrder!=0;
			}
		};
		moveUpAction.putValue(Action.SHORT_DESCRIPTION, "Moves this item up into the list.");
        add(moveUpAction);
        
        //Move Down
        Action moveDownAction = new AbstractAction("Move Down",ResourcesFactory.getMoveDownIcon()) {
			
			private static final long serialVersionUID = 7430912940318934757L;

			@Override
			public void actionPerformed(ActionEvent e) {
				IndividualScriptPanel.this.firePropertyChange(MOVE_DOWN_INDIVIDUAL_SCRIPT, null, indexOrder);
				
			}
		};
		moveDownAction.putValue(Action.SHORT_DESCRIPTION, "Moves this item down into the list.");
        add(moveDownAction);
	}

	/**
	 * @return the indexOrder
	 */
	public int getIndexOrder() {
		return indexOrder;
	}

	/**
	 * @param indexOrder the indexOrder to set
	 */
	public void setIndexOrder(int indexOrder) {
		this.indexOrder = indexOrder;
	}

}
